package ext.generic.wfaugment;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import wt.fc.Persistable;

import wt.inf.container.WTContainer;

import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;

import wt.project.Role;

import wt.team.Team;
import wt.team.TeamManaged;
import wt.team.TeamReference;

import wt.util.WTException;
import wt.util.WTProperties;

import wt.workflow.definer.WfAssignedActivityTemplate;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WfActorRoleAssignee;
import wt.workflow.work.WorkItem;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.AttributeConfig;
import com.ptc.mvc.components.AttributePanelConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.GroupConfig;
import com.ptc.mvc.util.ClientMessageSource;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;

@ComponentBuilder("generic.workflowStepInfor")
public class WorkflowStepInforPanelBuilder extends AbstractComponentBuilder
{
    private ClientMessageSource msgSource;
    private static boolean VERBOSE = false;

    private static WTProperties wtproperties;
    static
    {
        wtproperties = null;
        try
        {
            wtproperties = WTProperties.getLocalProperties();
            VERBOSE = wtproperties.getProperty("ext.generic.workflowSteps.verbose", false);
        }
        catch (Throwable throwable)
        {
            throwable.printStackTrace();
        }
    }

    public WorkflowStepInforPanelBuilder()
    {
        msgSource = getMessageSource("ext.generic.wfaugment.workflowStepRB");
    }

    protected AttributePanelConfig buildAttributePanelConfig(ComponentParams componentparams) throws WTException
    {

        ComponentConfigFactory componentconfigfactory = getComponentConfigFactory();
        AttributePanelConfig attributepanelconfig = componentconfigfactory.newAttributePanelConfig();

        GroupConfig groupconfig = componentconfigfactory.newGroupConfig("WORKFLOWGUID", msgSource.getMessage("WORKFLOWGUID"), Integer.valueOf(1));
        //GroupConfig groupconfig = componentconfigfactory.newGroupConfig("WORKFLOWGUID");

        AttributeConfig defalutattributeconfig = componentconfigfactory.newAttributeConfig("defalut", "", 0, 0);
        //AttributeConfig defalutattributeconfig = componentconfigfactory.newAttributeConfig("defalut");
        groupconfig.addComponent(defalutattributeconfig);
        //groupconfig.
        try
        {
            //System.out.println("》》》》》》》》》》》》》22");
            NmHelperBean nmhelperbean = ((JcaComponentParams) componentparams).getHelperBean();
            NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();

            if (nmcommandbean != null && nmcommandbean.getActionOid() != null)
            {
                NmOid nmoid = nmcommandbean.getActionOid();
                Object obj = nmcommandbean.getActionOid().getRefObject();
                if (VERBOSE)
                    System.out.println(".....WorkflowStepInforPanelBuilder....buildAttributePanelConfig==" + obj);
                if (obj instanceof WorkItem)
                {
                    WorkItem workItem = (WorkItem) obj;
                    Persistable persistable = workItem.getPrimaryBusinessObject().getObject();
                    WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
                    WfProcess wfprocess = wfactivity.getParentProcess();
                    Hashtable activeht = WorkFlowStepUtil.getWorkFlowStepInReviewGuideByName(persistable, wfprocess);
                    String wftempname = wfprocess.getTemplate().getName();
                    Hashtable stepordeht = WorkFlowStepOrderConstant.workfoworder;
                    if (stepordeht != null && stepordeht.get(wftempname) != null)
                    {
                        String[] activeorde = (String[]) stepordeht.get(wftempname);
                        if (VERBOSE)
                            System.out.println(" WorkflowStepInforPanelBuilder--buildAttributePanelConfig-activeorder= " + activeorde);
                        int cul = 0;
                        for (int i = 0; i < activeorde.length; i++)
                        {
                            String tempname = activeorde[i];
                            if (tempname != null && tempname.length() > 0 && activeht.get(tempname) != null)
                            {
                                String dtid = "step_" + cul;
                                if (VERBOSE)
                                {
                                    System.out.println(" WorkflowStepInforPanelBuilder--buildAttributePanelConfig-attributeconfig= " + dtid);
                                    System.out.println(" WorkflowStepInforPanelBuilder--buildAttributePanelConfig-wftempname= " + tempname);
                                }
                                AttributeConfig attributeconfig = componentconfigfactory.newAttributeConfig(dtid, "", 1, cul);
                                //AttributeConfig attributeconfig = componentconfigfactory.newAttributeConfig(dtid);
                                attributeconfig.setDataUtilityId(dtid);

                                attributeconfig.setColSpan(Integer.valueOf(3));
                                groupconfig.addComponent(attributeconfig);
                                cul++;
                            }
                        }
                    }
                }
            }
            attributepanelconfig.addComponent(groupconfig);
        }
        catch (Exception e)
        {
            throw new WTException(e);
        }
        return attributepanelconfig;
    }

    public Object buildComponentData(ComponentConfig componentconfig, ComponentParams componentparams) throws Exception
    {
        //WorkFlowStepInforPanelObject panelobje =null;
        NmHelperBean nmhelperbean = ((JcaComponentParams) componentparams).getHelperBean();
        HashMap ht = new HashMap();
        NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();

        if (nmcommandbean != null && nmcommandbean.getActionOid() != null)
        {
            NmOid nmoid = nmcommandbean.getActionOid();
            Object obj = nmcommandbean.getActionOid().getRefObject();
            if (obj instanceof WorkItem)
            {
                WTContainer container = nmcommandbean.getContainer();
                StringBuffer rolebelong = new StringBuffer("");
                WorkItem workItem = (WorkItem) obj;
                Persistable persistable = workItem.getPrimaryBusinessObject().getObject();
                WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
                WfProcess wfprocess = wfactivity.getParentProcess();
                Team team = (Team) ((TeamReference) ((TeamManaged) wfprocess).getTeamId()).getObject();
                Hashtable activeInprocess = WorkFlowStepUtil.getAllActivitysHashtableInProcess(wfprocess);
                Hashtable activehtInguide = WorkFlowStepUtil.getWorkFlowStepInReviewGuideByTemplate(persistable, wfprocess);
                Hashtable activehtIntemplate = WorkFlowStepUtil.getAllActivitysAndRoleInTemplate(wfprocess, persistable);
                Vector setroleset = WorkFlowStepUtil.getNeedSetRole(wfactivity);
                String wftempname = wfprocess.getTemplate().getName();
                if (VERBOSE)
                    System.out.println(" WorkflowStepInforPanelBuilder--buildComponentData--wftempname= " + wftempname);
                Hashtable stepordeht = WorkFlowStepOrderConstant.workfoworder;
                if (stepordeht != null && stepordeht.get(wftempname) != null)
                {
                    String[] activeorde = (String[]) stepordeht.get(wftempname);
                    if (VERBOSE)
                        System.out.println(" WorkflowStepInforPanelBuilder--buildComponentData--activeorde.length= " + activeorde.length);
                    int k = 0;
                    for (int i = 0; i < activeorde.length; i++)
                    {
                        String tempname = activeorde[i];
                        if (tempname != null && tempname.length() > 0)
                        {

                            for (Iterator it = activehtInguide.keySet().iterator(); it.hasNext(); )
                            {
                                WfAssignedActivityTemplate acttemplate = (WfAssignedActivityTemplate) it.next();
                                String acttemplatename = acttemplate.getName();
                                String username = "";
                                Role role = null;
                                if (acttemplatename.equals(tempname))
                                {
                                    String roledefaul = (String) activehtInguide.get(acttemplate);
                                    Object tempobject = activehtIntemplate.get(acttemplate);
                                    if (tempobject instanceof Role)
                                    {
                                        role = (Role) tempobject;
                                        StringBuffer strb = new StringBuffer("");
                                        if (team != null && role != null)
                                        {
                                            Enumeration userem = team.getPrincipalTarget(role);
                                            if (userem != null)
                                            {
                                                while (userem.hasMoreElements())
                                                {
                                                    WTPrincipalReference principalRef = (WTPrincipalReference) userem.nextElement();
                                                    WTPrincipal wtpr = principalRef.getPrincipal();
                                                    rolebelong = WorkFlowStepUtil.getRolesOfUser(container, wtpr);
                                                    strb.append(((WTUser) wtpr).getFullName() + "(" + rolebelong.toString() + ")\n");
                                                }
                                            }
                                        }
                                        username = strb.toString();
                                    }
                                    else if (tempobject instanceof WfActorRoleAssignee)
                                    {
                                        WfActorRoleAssignee wfr = (WfActorRoleAssignee) tempobject;
                                        username = wfr.getActorRole().getFullDisplay();
                                    }
                                    else
                                        username = (String) activehtIntemplate.get(acttemplate);
                                    WfActivity wfa = (WfActivity) activeInprocess.get(acttemplatename);

                                    ht.put("step_" + k + "_workItem", workItem);
                                    ht.put("step_" + k + "_team", team);
                                    //当前流程节点
                                    ht.put("step_" + k + "_currentwfactivity", wfactivity);
                                    ht.put("step_" + k + "_wfactivity", wfa);
                                    ht.put("step_" + k + "_role", role);
                                    ht.put("step_" + k + "_wfAssignedActivityTemplate", acttemplate);
                                    ht.put("step_" + k + "_user", username.toString());
                                    ht.put("step_" + k + "_roledefault", roledefaul);
                                    ht.put("step_" + k + "_setroleset", setroleset);

                                    k++;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return ht;
    }

    public ComponentConfig buildComponentConfig(ComponentParams componentparams) throws WTException
    {
        return buildAttributePanelConfig(componentparams);
    }
}
